package com.spsoft.vatoutinv.service.vmsavalibletaxrate.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spsoft.core.model.Result;
import com.spsoft.core.utils.BeanUtils;
import com.spsoft.core.utils.ExcelUtils;
import com.spsoft.vatoutinv.constants.DictConstant;
import com.spsoft.vatoutinv.entity.vmsavalibletaxrate.VmsAvalibleTaxRateLog;
import com.spsoft.vatoutinv.mapper.VmsAvalibleTaxRateLogMapper;
import com.spsoft.vatoutinv.model.dto.vmsavalibletaxrate.VmsAvalibleTaxRateLogBaseDto;
import com.spsoft.vatoutinv.model.vo.vmsavalibletaxrate.VmsAvalibleTaxRateVo;
import com.spsoft.vatoutinv.service.translate.impl.DictTranslate;
import com.spsoft.vatoutinv.service.vmsavalibletaxrate.VmsAvalibleTaxRateLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 可用税率信息查询记录列表表 服务实现类
 * </p>
 *
 * @author chenxudong
 * @since 2023-11-23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class VmsAvalibleTaxRateLogServiceImpl extends ServiceImpl<VmsAvalibleTaxRateLogMapper, VmsAvalibleTaxRateLog> implements VmsAvalibleTaxRateLogService {

    public static final String LOG_MSG = "(可用税率信息获取记录详情实现层)";

    private final DictTranslate dictTranslate;

    @Override
    public Result<IPage<VmsAvalibleTaxRateVo>> queryVmsAvalibleTaxRateLogDetail(VmsAvalibleTaxRateLogBaseDto vmsAvalibleTaxRateLogBaseDto) {
        // 获取分页数据
        Page<VmsAvalibleTaxRateLog> page = new Page<>(vmsAvalibleTaxRateLogBaseDto.getPageNo(), vmsAvalibleTaxRateLogBaseDto.getPageSize());
        LambdaQueryWrapper<VmsAvalibleTaxRateLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VmsAvalibleTaxRateLog::getLogBaseId, vmsAvalibleTaxRateLogBaseDto.getLogBaseId());
        queryWrapper.orderByDesc(VmsAvalibleTaxRateLog::getId);
        Page<VmsAvalibleTaxRateLog> IPage = this.baseMapper.selectPage(page, queryWrapper);
        // 将数据库返回数据转换为VO返回前端
        IPage<VmsAvalibleTaxRateVo> IPageVo = IPage.convert(item -> {
            VmsAvalibleTaxRateVo vo = new VmsAvalibleTaxRateVo();
            // 转换数据
            BeanUtils.copyBeanProp(vo, item);
            return vo;
        });
        return Result.OK(IPageVo);
    }

    @Override
    public void exportVmsAvalibleTaxRateLogDetail(Long logBaseId, HttpServletResponse response) {
        try {
            LambdaQueryWrapper<VmsAvalibleTaxRateLog> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VmsAvalibleTaxRateLog::getLogBaseId, logBaseId);
            queryWrapper.orderByDesc(VmsAvalibleTaxRateLog::getId);
            List<VmsAvalibleTaxRateLog> vmsAvalibleTaxRateLogList = this.list(queryWrapper);
            // 字段转换
            List<VmsAvalibleTaxRateVo> vmsAvalibleTaxRateVoList = BeanUtils.copyListProperties(vmsAvalibleTaxRateLogList, VmsAvalibleTaxRateVo::new, (s, t) -> {
                // 导出时需要将字典值进行转换
                t.setCsxz(dictTranslate.translate(DictConstant.VMS_KYSLCSXZ, s.getCsxz()));
                t.setYslbz(dictTranslate.translate(DictConstant.VMS_YSLBZ, s.getYslbz()));
                t.setCszt(dictTranslate.translate(DictConstant.VMS_KYSLCSZT, s.getCszt()));
            });
            // 添加序号
            for (int i = 0; i < vmsAvalibleTaxRateVoList.size(); i++) {
                vmsAvalibleTaxRateVoList.get(i).setXh(i + 1);
            }
            // 导出
            ExcelUtils.exportExcel(vmsAvalibleTaxRateVoList, VmsAvalibleTaxRateVo.class, "可用税率获取记录", response);
        } catch (Exception e) {
            log.error("{}出现异常", LOG_MSG, e);
        }
    }

}
